#include <Wire.h>
#include <MsTimer2.h>
#include "I2C_RTC.h"
#include <string.h>
#include <stdint.h>
#include <avr/wdt.h>
#include <LedControl.h>

#define TIMES_PERIOD 1  //定时时间间隔，ms
#define LOG_INFO(s) Serial.println(s);
#define LOG_INFO_NONLN(ss) Serial.print(ss);
#define LOG_INT(var) Serial.println(var,DEC);
#define LOG_INT_NONLN(vars) Serial.print(vars,DEC);

//点阵屏编码
unsigned char T9_[]={
                    B00000000,
                    B01110000,
                    B01010000,
                    B01110000,
                    B00010000,
                    B01110000,
                    B00000000,
                    B00000000};//9_
unsigned char T8_[]={
                    B00000000,
                    B01110000,
                    B01010000,
                    B01110000,
                    B01010000,
                    B01110000,
                    B00000000,
                    B00000000};//8_
unsigned char T7_[]={
                    B00000000,
                    B01110000,
                    B00010000,
                    B00010000,
                    B00010000,
                    B00010000,
                    B00000000,
                    B00000000};//7_
unsigned char T6_[]={
                    B00000000,
                    B01110000,
                    B01000000,
                    B01110000,
                    B01010000,
                    B01110000,
                    B00000000,
                    B00000000};//6_
unsigned char T5_[]={
                    B00000000,
                    B01110000,
                    B01000000,
                    B01110000,
                    B00010000,
                    B01110000,
                    B00000000,
                    B00000000};//5_
unsigned char T4_[]={
                    B00000000,
                    B01010000,
                    B01010000,
                    B01110000,
                    B00010000,
                    B00010000,
                    B00000000,
                    B00000000};//4_
unsigned char T3_[]={
                    B00000000,
                    B01110000,
                    B00010000,
                    B01110000,
                    B00010000,
                    B01110000,
                    B00000000,
                    B00000000};//3_

unsigned char T2_[]={
                    B00000000,
                    B01110000,
                    B00010000,
                    B01110000,
                    B01000000,
                    B01110000,
                    B00000000,
                    B00000000};//2_

unsigned char T1_[]={
                    B00000000,
                    B00100000,
                    B00100000,
                    B00100000,
                    B00100000,
                    B00100000,
                    B00000000,
                    B00000000};//1_

unsigned char T0_[]={
                    B00000000,
                    B01110000,
                    B01010000,
                    B01010000,
                    B01010000,
                    B01110000,
                    B00000000,
                    B00000000};//0_
                    
unsigned char T_9[]={
                    B00000000,
                    B00000111,
                    B00000101,
                    B00000111,
                    B00000001,
                    B00000111,
                    B00000000,
                    B00000000};//_9
unsigned char T_8[]={
                    B00000000,
                    B00000111,
                    B00000101,
                    B00000111,
                    B00000101,
                    B00000111,
                    B00000000,
                    B00000000};//_8
unsigned char T_7[]={
                    B00000000,
                    B00000111,
                    B00000001,
                    B00000001,
                    B00000001,
                    B00000001,
                    B00000000,
                    B00000000};//_7
unsigned char T_6[]={
                    B00000000,
                    B00000111,
                    B00000100,
                    B00000111,
                    B00000101,
                    B00000111,
                    B00000000,
                    B00000000};//_6
unsigned char T_5[]={
                    B00000000,
                    B00000111,
                    B00000100,
                    B00000111,
                    B00000001,
                    B00000111,
                    B00000000,
                    B00000000};//_5
unsigned char T_4[]={
                    B00000000,
                    B00000101,
                    B00000101,
                    B00000111,
                    B00000001,
                    B00000001,
                    B00000000,
                    B00000000};//_4
unsigned char T_3[]={
                    B00000000,
                    B00000111,
                    B00000001,
                    B00000111,
                    B00000001,
                    B00000111,
                    B00000000,
                    B00000000};//_3
unsigned char T_2[]={
                    B00000000,
                    B00000111,
                    B00000001,
                    B00000111,
                    B00000100,
                    B00000111,
                    B00000000,
                    B00000000};//_2
unsigned char T_1[]={
                    B00000000,
                    B00000010,
                    B00000010,
                    B00000010,
                    B00000010,
                    B00000010,
                    B00000000,
                    B00000000};//_1
unsigned char T_0[]={
                    B00000000,
                    B00000111,
                    B00000101,
                    B00000101,
                    B00000101,
                    B00000111,
                    B00000000,
                    B00000000};//_0                  
byte Love[] = {
                    B00000000,
                    B01101100,
                    B11111110,
                    B11111110,
                    B01111100,
                    B00111000,
                    B00010000,
                    B00000000};//心型

byte Month_CH[] = {     
                    B00111100,
                    B00100100,
                    B00111100,
                    B00100100,
                    B00111100,
                    B00100100,
                    B01000100,
                    B00000000};//月 
byte Day_CH[] = {
                    B00111100,
                    B00100100,
                    B00100100,
                    B00111100,
                    B00100100,
                    B00100100,
                    B00111100,
                    B00000000};//日   
					
//byte Monday_CH[]={0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00};//一
//byte Tuesday_CH[]={0x00,0x00,0x3C,0x00,0x00,0x00,0x7E,0x00};//二
//byte Wednesday_CH[]={0x00,0x00,0x3C,0x00,0x3C,0x00,0x7E,0x00};//三
//byte Thursday_CH[]={0x00,0x7E,0x5A,0x5A,0x6E,0x42,0x7E,0x00};//四 
//byte Friday_CH[]={0x00,0x7C,0x10,0x7C,0x14,0x14,0x7E,0x00};//五
//byte Saturday_CH[]={0x00,0x10,0x08,0x7E,0x00,0x24,0x42,0x00};//六
//byte Sunday_CH[]={0x00,0x10,0x10,0x7E,0x10,0x10,0x1E,0x00};//七   


//#define DEBUG_LED

enum _GRAY{
  GRAY_LEVEL1 =0x88,
  GRAY_LEVEL2,
  GRAY_LEVEL3,
  GRAY_LEVEL4,
  GRAY_LEVEL5,
  GRAY_LEVEL6,
  GRAY_LEVEL7,
  GRAY_LEVEL8
}GRAY;

//任务结构
typedef struct _TASK_COMPONENTS
{
  int Run; //程序运行标记： 0-不运行，1-运行
  int Timer; //计时器
  int ItvTime;  //任务运行时间间隔
  void (*TaskHook)(void); //要运行的任务函数
}TASK_COMPONENTS;//任务定义,TASK_COMPONENTS是_TASK_COMPONENTS的别名

void TaskUpdateTime();
void TaskReadRTC();
void TaskRemarks();

static TASK_COMPONENTS TaskComps[]=
{
  {0,1000,1000,TaskUpdateTime},  //更新静态显示的时间
  {0,200,200,TaskReadRTC},//读取RTC时间
};

//任务清单
typedef enum _TASK_LIST
{
  TASK_UPDATE_TIME,
  TASK_READ_RTC,
  TASKS_MAX   //总共可分配的定时任务数目
}TASK_LIST;

typedef struct now_date{ //实际时间
uint8_t t_hour;
uint8_t t_min;
uint8_t t_sec;
uint16_t t_year;
uint8_t t_month;
uint8_t t_day;
uint8_t t_week;   
}NOW_DATE;

NOW_DATE now_date; //当前时间结构体变量
NOW_DATE temp_date; //临时时间结构体变量
String timedata = "";


static DS3231 myclock;//define a object of DS1307 class

uint8_t test_led_pin = 13;//状态指示灯引脚

void idel_task();
void (*func)(void)=idel_task;//函数指针

unsigned char (*p)[10];//数组指针
unsigned char *sp_x[10]={T_0,T_1,T_2,T_3,T_4,T_5,T_6,T_7,T_8,T_9};//指针数组
unsigned char *spx_[10]={T0_,T1_,T2_,T3_,T4_,T5_,T6_,T7_,T8_,T9_};//指针数组
//unsigned char *weekx_[8]={0x00,Monday_CH,Tuesday_CH,Wednesday_CH,Thursday_CH,Friday_CH,Saturday_CH,Sunday_CH};//指针数组

//显示日期的点阵屏
LedControl g_lc = LedControl(4, 5, 6, 4);//(int dataPin, int clkPin, int csPin, int numDevices)

//显示时间的大数码管
LedControl lc=LedControl(12,11,10,4); //(int dataPin, int clkPin, int csPin, int numDevices)
 
//显示两点的小数码管
const uint8_t dio_dpdp_pin = 7;
const uint8_t clk_dpdp_pin = 8;
const uint8_t stb_dpdp_pin = 3;
const uint8_t disp_pos[]={0xc0,0xc2,0xc4,0xc6};

//显示温度的两位中数码管
const uint8_t dataM_pin = 9;
const uint8_t clkM_pin = 2;
const uint8_t loadM_pin = 14;

//光检测模块引脚
const uint8_t intensity_pin = 2; //模拟输入脚 A2

//显示星期的小数码管
const uint8_t dio_week_pin = 15;
const uint8_t clk_week_pin = 16;
const uint8_t stb_week_pin = 17;

//18b20连接的管脚
//const uint8_t one_wire_pin = 2; //数字脚 2

uint8_t seg_light_value = 8;//大数码管亮度值 0-15,数值越大，亮度越亮
uint8_t matrix_light_value = 8;//点阵屏亮度值 0-15,数值越大，亮度越亮

#define V_NUM 30
const uint8_t seg_cool[V_NUM]={0x40,0x60,0x70,0x38,0x1c,0x0e,0x83,0x81,0x80,0x00,\
							0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
							0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//共阳大数码管，跑灯效果
							
#define NUM 12
const uint8_t seg[NUM]={0xfe,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x00,0x80};//共阳大数码管，0-9,不亮,dp
const uint8_t segM[NUM]={0xC0,0xF9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x7f};//共阴，0-9,不亮,dp

const uint8_t segW[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴，0-9
boolean seg_dpoint_off_on = false;
  
uint8_t index=0;
char rev_ch = 0; //必须是char
String rev_str = "";

//OneWire oneWire(one_wire_pin); //声明连接在单总线上的单总线设备
//DallasTemperature sensors(&oneWire);//声明一个传感器对象
int temperatures_value = 0;

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*******************************************************************/
/*  系统初始化函数                                                     */
/*******************************************************************/
void setup()
{
  seg_init();  //大数码管显示时间
  seg_dpdp_init();//小数码管显示两个点
  seg_week_init();//小数码管显示星期
  matrix_screen_init();//点阵屏显示心型 
  segM_init();  //中数码管显示温度
//  while(1); 
  
  delay(500); 
  #ifdef DEBUG_LED 
    pinMode(test_led_pin,OUTPUT);
  #endif
  MsTimer2::set(TIMES_PERIOD, TaskRemarks);//定时器2影响analogWrite输出PWM，因为都用定时器2
  MsTimer2::start();

  myclock.begin();//启动RTC
  myclock.startClock(); //获取RTC时间
  
  delay(500);

    now_date.t_hour = myclock.getHours(); //小时
    now_date.t_min = myclock.getMinutes(); //分钟
    now_date.t_sec = myclock.getSeconds(); //秒
    now_date.t_year = myclock.getYear(); //年
    now_date.t_month = myclock.getMonth(); //月
    now_date.t_day = myclock.getDay(); //日
    now_date.t_week = myclock.getWeek(); //星期  

  if(now_date.t_hour > 23 || now_date.t_min > 59 || now_date.t_sec > 59 || now_date.t_year > 2100 || now_date.t_month > 12 || now_date.t_day > 31) 
  {
            myclock.setHourMode(CLOCK_H24);
//            myclock.setDate(14,3,2024);
//            myclock.setTime(22,52,0);
//            myclock.setWeek(4);
  myclock.setDay(17);
  myclock.setMonth(3);
  myclock.setYear(2024);

  myclock.setHours(18);
  myclock.setMinutes(14);
  myclock.setSeconds(00);

  myclock.setWeek(7);
  }
   delay(500);
  //点阵屏显示日期
  matrix_screen_dispdate(now_date.t_year,now_date.t_month,now_date.t_day);//年月日
  matrix_screen_dispweek(now_date.t_week);//星期
  
  temperatures_value = myclock.getTemp();
  matrix_screen_disptemperature(temperatures_value);
  temp_date = now_date;
  //wdt_init();
  Serial.begin(9600);
  Serial.println("system init finish");
}
/*******************************************************************/
/*  主循环函数                                                     */
/*******************************************************************/
void loop()
{
  TaskProcess();
  if(Serial.available()>0){//有串口数据
    rev_ch = Serial.read();
//    Serial.print(rev_ch);
    rev_str += rev_ch;
    index++;
    //settime=2022121123012001@
    if(rev_ch == '@'){
       if(index == 25){
//         Serial.println(rev_str);
//         Serial.println(rev_str.substring(0,8));
         if(rev_str.substring(0,8) == "settime="){ 
            temp_date.t_year = (rev_str.substring(8,12)).toInt(); 
            temp_date.t_month = (rev_str.substring(12,14)).toInt();
            temp_date.t_day = (rev_str.substring(14,16)).toInt();
            temp_date.t_hour = (rev_str.substring(16,18)).toInt();
            temp_date.t_min = (rev_str.substring(18,20)).toInt();
            temp_date.t_sec = (rev_str.substring(20,22)).toInt();
            temp_date.t_week = (rev_str.substring(22,24)).toInt();
           //设置RTC时间
//            myclock.fillByYMD(temp_date.t_year,temp_date.t_month,temp_date.t_day);
//            myclock.fillByHMS(temp_date.t_hour,temp_date.t_min,temp_date.t_sec);
//            myclock.fillDayOfWeek(temp_date.t_week);
//            myclock.setTime();//设置RTC时间  

            myclock.setDate(temp_date.t_day,temp_date.t_month,temp_date.t_year);
            myclock.setTime(temp_date.t_hour,temp_date.t_min,temp_date.t_sec);
            myclock.setWeek(temp_date.t_week);
            
            Serial.println("set time success");     
            TaskUpdateTime(); //更新时间显示
         }        
       }
       index = 0;
       rev_ch = 0;
       rev_str = "";
    }      
  }
}

/*******************************************************************/
/*  任务进程函数                                                     */
/*******************************************************************/
void TaskProcess()
{
   static unsigned char i=0;  //必须是static类型

   for(i=0;i<TASKS_MAX;i++) //逐个任务时间处理
   {
    if(TaskComps[i].Run)  //任务可以运行
    {
       TaskComps[i].TaskHook();  //调用任务函数
       TaskComps[i].Run = 0; //标志清0
    }
   }
}

/*******************************************************************/
/*  更新时间显示任务函数                                                     */
/*******************************************************************/
void TaskUpdateTime()
{
  //LOG_INFO("update_time");
    #ifdef DEBUG_LED 
      digitalWrite(test_led_pin,!(digitalRead(test_led_pin)));
    #endif    
    temp_date.t_hour = now_date.t_hour;
    temp_date.t_min = now_date.t_min;
    temp_date.t_sec = now_date.t_sec; 
//    serial_log_disptime(); 

//    LED_gray_adjust();//亮度调节
     
    seg_dis_time(temp_date.t_hour,temp_date.t_min,temp_date.t_sec); //数码管显示时间

    if(temp_date.t_sec == 59){//每分钟更新一次温度
      temperatures_value = myclock.getTemp();
      matrix_screen_disptemperature(temperatures_value);
      //LOG_INFO("temperatures: ");
      //LOG_INT(temperatures_value);       
      Serial.println("settime=2022121123012001@"); 
    }
    if(temp_date.t_day != now_date.t_day){//每天更新一次日期
      temp_date.t_year = now_date.t_year;
      temp_date.t_month = now_date.t_month;
      temp_date.t_day = now_date.t_day;
      matrix_screen_dispdate(temp_date.t_year,temp_date.t_month,temp_date.t_day);//点阵屏显示日期
      temp_date.t_week = now_date.t_week;
      matrix_screen_dispweek(temp_date.t_week);//点阵屏显示星期
      //LOG_INFO("now_date");
    }
}

/*******************************************************************/
/*  读取RTC时间值任务函数                                                     */
/*******************************************************************/
void TaskReadRTC()
{
    
//    myclock.getTime(); //获取RTC时间
//    now_date.t_hour = myclock.hour; //小时
//    now_date.t_min = myclock.minute; //分钟
//    now_date.t_sec = myclock.second; //秒
//    now_date.t_year = myclock.year; //年
//    now_date.t_month = myclock.month; //月
//    now_date.t_day = myclock.dayOfMonth; //日
//    now_date.t_week = myclock.dayOfWeek; //星期   

    now_date.t_hour = myclock.getHours(); //小时
    now_date.t_min = myclock.getMinutes(); //分钟
    now_date.t_sec = myclock.getSeconds(); //秒
    now_date.t_year = myclock.getYear(); //年
    now_date.t_month = myclock.getMonth(); //月
    now_date.t_day = myclock.getDay(); //日
    now_date.t_week = myclock.getWeek(); //星期    
}
/*******************************************************************/
/*  大数码管显示初始化函数                                                     */
/*******************************************************************/
void seg_init()
{
  uint8_t codes[]={0x06,0x48,0x30,0x80,0x00};
   for(int i=0;i<4;i++)
   {
      lc.shutdown(i,false);
      lc.setIntensity(i,10);//亮度0-15
   }  
   for(int i=0;i<4;i++)
   {
      for(int j=0;j<5;j++)
      {
        lc.setRow(i,0,codes[j]);
        delay(100);
      }
      delay(100);
   }
   
   for(int i=0;i<4;i++)
   {
      //lc.clearDisplay(i);
      for(int j=0;j<4;j++)
      {
        lc.setRow(i,0,0x01);
        delay(100);
      }
      delay(100);
   } 

}
/*******************************************************************/
/*  点阵屏显示初始化                                                     */
/*******************************************************************/
void matrix_screen_init()
{
  uint8_t k=0,j=0;

  for(k=0;k<4;k++) 
    g_lc.shutdown(k,false);
    
  g_lc.setIntensity(0, 1);
  g_lc.setIntensity(1, 3);
  g_lc.setIntensity(2, 1);
  g_lc.setIntensity(3, 3);

  for(k=0;k<4;k++) 
      g_lc.clearDisplay(k);
  
  for(k=0;k<4;k++){   
    for(j=0;j<8;j++)
      g_lc.setRow(k, j, Love[j]); //心型
  }  
}
/********************************************************************************/
//显示温度的数码管初始化
void segM_init()
{
  //引脚配置为输出
  pinMode(dataM_pin,OUTPUT);
  pinMode(clkM_pin,OUTPUT);
  pinMode(loadM_pin,OUTPUT);

  digitalWrite(loadM_pin,LOW);
  shiftOut(dataM_pin,clkM_pin,MSBFIRST,segM[8] & segM[11]);//个位
  delay(20);//延迟很重要,显示不闪烁
  shiftOut(dataM_pin,clkM_pin,MSBFIRST,segM[8] & segM[11]);//十位
  delay(20);//延迟很重要,显示不闪烁
  digitalWrite(loadM_pin,HIGH);  

}
/*******************************************************************/
/*  小数码管显示两个圈                                                     */
/*******************************************************************/
void seg_dpdp_init()
{
  pinMode(dio_dpdp_pin,OUTPUT);
  pinMode(clk_dpdp_pin,OUTPUT);
  pinMode(stb_dpdp_pin,OUTPUT);  
  digitalWrite(dio_dpdp_pin,HIGH);
  digitalWrite(clk_dpdp_pin,HIGH);
  digitalWrite(stb_dpdp_pin,HIGH);
  
  tm1628_cleardisplay();
  tm1628_wrcom(GRAY_LEVEL1);//亮度1级
  tm1628_display(disp_pos[0],0x5c);//显示两个圈
  tm1628_display(disp_pos[1],0x00);
  tm1628_display(disp_pos[2],0x63);
  tm1628_display(disp_pos[3],0x00);
}
/***********显示星期的小数码管**************************************************/
void seg_week_init()
{
  pinMode(dio_week_pin,OUTPUT);
  pinMode(clk_week_pin,OUTPUT);
  pinMode(stb_week_pin,OUTPUT);  
  digitalWrite(dio_week_pin,HIGH);
  digitalWrite(clk_week_pin,HIGH);
  digitalWrite(stb_week_pin,HIGH);
  
  tm1628W_cleardisplay();
  tm1628W_wrcom(GRAY_LEVEL2);//亮度2级
  tm1628W_display(disp_pos[0],0x40);//显示-
  tm1628W_display(disp_pos[1],0x00);
  tm1628W_display(disp_pos[2],0x40);//显示-
  tm1628W_display(disp_pos[3],0x00);
}

/*写数据*/
void tm1628_writedata(uint8_t cmd)
{
  uint8_t i;
  for(i=0;i<8;i++){
    if(cmd & 0x01)
    {
       digitalWrite(dio_dpdp_pin,HIGH); 
    }else{
       digitalWrite(dio_dpdp_pin,LOW);
    }
    digitalWrite(clk_dpdp_pin,LOW);
    delayMicroseconds(10);
    cmd = cmd >> 1;
    digitalWrite(clk_dpdp_pin,HIGH);
  }
  delayMicroseconds(40);
}
/*写命令*/
void tm1628_wrcom(uint8_t com){
  digitalWrite(stb_dpdp_pin,HIGH);
  delayMicroseconds(5);
  digitalWrite(stb_dpdp_pin,LOW);
  delayMicroseconds(2);
  tm1628_writedata(com);
  delayMicroseconds(2);
}

/*指定位置写数据*/
void tm1628_addrdat(uint8_t addr, uint8_t dat){
  digitalWrite(stb_dpdp_pin,HIGH);
  delayMicroseconds(5);
  digitalWrite(stb_dpdp_pin,LOW);
  delayMicroseconds(2);
  tm1628_writedata(addr);
  tm1628_writedata(dat);
  delayMicroseconds(2);
}

/*固定写显示*/
void tm1628_display(uint8_t addr, uint8_t val){
  tm1628_wrcom(0x03);
  tm1628_wrcom(0x44);

  tm1628_addrdat(addr,val);

  digitalWrite(stb_dpdp_pin,HIGH);
  delayMicroseconds(100);
}

/*清除显示*/
void tm1628_cleardisplay(void)
{
  uint8_t const CODEs[]={0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc};
  
  tm1628_wrcom(0x03);
  //
  for(uint8_t i=0;i<7;i++)//7位
  {
    tm1628_wrcom(CODEs[i]);
    tm1628_writedata(0x00);
    digitalWrite(stb_dpdp_pin,HIGH);
  }
}


/*写数据*/
void tm1628W_writedata(uint8_t cmd)
{
  uint8_t i;
  for(i=0;i<8;i++){
    if(cmd & 0x01)
    {
       digitalWrite(dio_week_pin,HIGH); 
    }else{
       digitalWrite(dio_week_pin,LOW);
    }
    digitalWrite(clk_week_pin,LOW);
    delayMicroseconds(10);
    cmd = cmd >> 1;
    digitalWrite(clk_week_pin,HIGH);
  }
  delayMicroseconds(40);
}
/*写命令*/
void tm1628W_wrcom(uint8_t com){
  digitalWrite(stb_week_pin,HIGH);
  delayMicroseconds(5);
  digitalWrite(stb_week_pin,LOW);
  delayMicroseconds(2);
  tm1628W_writedata(com);
  delayMicroseconds(2);
}

/*指定位置写数据*/
void tm1628W_addrdat(uint8_t addr, uint8_t dat){
  digitalWrite(stb_week_pin,HIGH);
  delayMicroseconds(5);
  digitalWrite(stb_week_pin,LOW);
  delayMicroseconds(2);
  tm1628W_writedata(addr);
  tm1628W_writedata(dat);
  delayMicroseconds(2);
}

/*固定写显示*/
void tm1628W_display(uint8_t addr, uint8_t val){
  tm1628W_wrcom(0x03);
  tm1628W_wrcom(0x44);

  tm1628W_addrdat(addr,val);

  digitalWrite(stb_week_pin,HIGH);
  delayMicroseconds(100);
}

/*清除显示*/
void tm1628W_cleardisplay(void)
{
  uint8_t const CODEs[]={0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc};
  
  tm1628W_wrcom(0x03);
  //
  for(uint8_t i=0;i<7;i++)//7位
  {
    tm1628W_wrcom(CODEs[i]);
    tm1628W_writedata(0x00);
    digitalWrite(stb_week_pin,HIGH);
  }
}

/*******************************************************************/
/*  空任务函数                                                     */
/*******************************************************************/
void idel_task()
{
  
}
/*******************************************************************/
/*  看门狗初始化                                                     */
/*******************************************************************/
void wdt_init()
{
//  get_mcusr();
  wdt_enable(WDTO_8S);//设置看门狗溢出时间
}

/*******************************************************************/
/*  亮度随光强变化而变化                                                     */
/*******************************************************************/
void intensity_to_light()
{
  uint16_t intensity_value = 0; 
  static uint8_t new_seg_light_value = 0;
  static uint8_t new_matrix_light_value = 0;
  
  intensity_value = analogRead(intensity_pin);
  LOG_INFO("intensity_value:");
  LOG_INT(intensity_value);


//  if(matrix_light_value != new_matrix_light_value){
//    matrix_light_value = new_matrix_light_value;
//    for(int k=0;k<8;k++){
//      g_lc.shutdown(k,false);//开显示
//      g_lc.setIntensity(k, matrix_light_value);//亮度
//    }
//    LOG_INFO("matrix_light_value:");
//    LOG_INT(matrix_light_value);
//  }
}

/*******************************************************************/
/*  任务标记函数                                                     */
/*******************************************************************/
void TaskRemarks()
{
   static unsigned char i=0;   //必须是static类型

   for(i=0;i<TASKS_MAX;i++)   //逐个任务处理
   {
     if(TaskComps[i].Timer) //时间不为0
     {
      TaskComps[i].Timer--; //减去一个节拍
      if(TaskComps[i].Timer<=0)//时间减完了
      {
       TaskComps[i].Timer = TaskComps[i].ItvTime;  //恢复计时器值，重新下一次
       TaskComps[i].Run = 1; //任务可以运行
      }
     }
   }
}

//大数码管显示时间
void seg_dis_time(uint8_t Hour,uint8_t Min,uint8_t Sec)
{
  uint8_t hour_H,hour_L;
  uint8_t min_H,min_L;

    hour_H = Hour/10;
    hour_L = Hour%10;
    min_H = Min/10;
    min_L = Min%10;
    
    
    //数码管显示小时和分钟
 
    lc.setDigit(0,0,hour_H,false);
	  lc.setDigit(1,0,hour_L,false);
    lc.setDigit(2,0,min_H,false);
    
  seg_dpoint_off_on = !seg_dpoint_off_on;
  lc.setDigit(3,0,min_L,seg_dpoint_off_on);
}

/*******************************************************************/
/*  点阵屏显示日期                                                     */
/*******************************************************************/
void matrix_screen_dispdate(uint8_t Year,uint8_t Month,uint8_t Day)
{
  uint8_t year_H,year_L;
  uint8_t month_H,month_L;
  uint8_t day_H,day_L;
//  if(Year<0 || Year > 2100) Year = 2100;
  if(Month<0 || Month > 12) Month = 1;
  if(Day<0 || Day > 31) Day = 1;

//    year_H = Year/10;
//    year_L = Year%10;
    month_H = Month/10;
    month_L = Month%10;
    day_H = Day/10;
    day_L = Day%10;
    
    for(uint8_t i=0;i<8;i++){ 
      g_lc.setRow(3, i, spx_[month_H][i]|sp_x[month_L][i]);
      g_lc.setRow(2, i, Month_CH[i]);
      g_lc.setRow(1, i, spx_[day_H][i]|sp_x[day_L][i]);
      g_lc.setRow(0, i, Day_CH[i]);
    }    
}

/*******************************************************************/
/*  显示星期                                                     */
/*******************************************************************/
void matrix_screen_dispweek(uint8_t Week)
{
  uint8_t week_H,week_L;
  
  if(Week<1 || Week > 7) Week = 1;

    week_H = Week/10;
    week_L = Week%10;
    
    tm1628W_cleardisplay();
//    tm1628W_wrcom(0x89);//亮度2级
    tm1628W_display(disp_pos[0],0x40);//显示-
    tm1628W_display(disp_pos[1],segW[week_L]);
    tm1628W_display(disp_pos[2],0x40);//显示-
    tm1628W_display(disp_pos[3],0x00);
}
/*******************************************************************/
/*  显示温度                                                    */
/*******************************************************************/
void matrix_screen_disptemperature(uint8_t Temperature)
{
  static uint8_t value_H, value_L;

  value_H = Temperature / 10;
  value_L = Temperature % 10;

  digitalWrite(loadM_pin,LOW);
  shiftOut(dataM_pin,clkM_pin,MSBFIRST,segM[value_L]);//个位
  delay(20);//延迟很重要,显示不闪烁
  shiftOut(dataM_pin,clkM_pin,MSBFIRST,segM[value_H]);//十位
  delay(20);//延迟很重要,显示不闪烁
  digitalWrite(loadM_pin,HIGH);  
}

void LED_gray_adjust(void)
{
      if((temp_date.t_hour == 6) && (temp_date.t_min == 0) && (temp_date.t_sec == 0))//早上6点
      {
         for(int i=0;i<4;i++){
        lc.setIntensity(i,12);//显示时间的大数码亮度
//        delay(20);
        } 
        g_lc.setIntensity(0, 3);
        g_lc.setIntensity(1, 12);
        g_lc.setIntensity(2, 3);
        g_lc.setIntensity(3, 12);  
        tm1628W_wrcom(GRAY_LEVEL6);//显示星期的数码管亮度 
        tm1628_wrcom(GRAY_LEVEL6);//显示两圆圈点的数码管亮度
      }
      if((temp_date.t_hour == 18) && (temp_date.t_min == 0) && (temp_date.t_sec == 0))//下午6点
      {
         for(int i=0;i<4;i++){
        lc.setIntensity(i,5);//显示时间的大数码亮度
//        delay(20);
        }      
        g_lc.setIntensity(0, 1);
        g_lc.setIntensity(1, 4);
        g_lc.setIntensity(2, 1);
        g_lc.setIntensity(3, 4); 
        tm1628W_wrcom(GRAY_LEVEL2);//显示星期的数码管亮度
        tm1628_wrcom(GRAY_LEVEL2);//显示两圆圈点的数码管亮度
      }
}
